package cn.lele.reggie.controller;

import cn.lele.reggie.common.R;
import cn.lele.reggie.dto.DishDto;
import cn.lele.reggie.entity.Category;
import cn.lele.reggie.entity.Dish;
import cn.lele.reggie.service.CategoryService;
import cn.lele.reggie.service.DishFlavorService;
import cn.lele.reggie.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import cn.lele.reggie.config.*;
/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询+条件查询+涉及dish和category两个表
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> sellectAll(Integer page,Integer pageSize,String name){
        Page<DishDto> pa=dishService.pageDishAndflavour(page,pageSize,name);
        return R.success(pa);
    }

    /**
     * 删除菜品和对应的口味数据
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "dish",allEntries = true)
    public R<String> delete(Long[] ids){
        dishService.deleteWithFlavour(ids);
        return R.success("删除菜品成功");
    }

    /**
     * 添加菜品+口味
     * @param dish
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "dish",key = "#dish.categoryId")
    public R<String> insert(@RequestBody DishDto dish){
        dishService.saveDish(dish);
        return R.success("添加菜品成功");
    }

    /**
     * 根据id查dish（dtodish+flavour），修改回显效果
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<DishDto> selectById(@PathVariable Long id){
        DishDto dishDto = dishService.getDishDtoById(id);
        return R.success(dishDto);
    }

    /**
     * 更新菜品dish+更新菜品口味
     * @param dishDto
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "dish",key = "#dishDto.categoryId")
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateDishAndFlavour(dishDto);
        return R.success("更新菜品成功");
    }

    /**
     * 修改菜品状态
     * @param statusId
     * @param ids
     * @return
     */
    @PostMapping("/status/{statusId}")
    @CacheEvict(cacheNames = "dish",allEntries = true)
    public R<String> updateStatus(@PathVariable Integer statusId,Long[] ids){
        dishService.updateStatus(statusId,ids);
        return R.success("修改状态成功");
    }

    /**
     * 前台用户根据分类id查所有菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    //@Cacheable(cacheNames = "dish",key = "#categoryId")
    public R<List<DishDto>> listDish(Long categoryId,Integer status){
        log.info("列出菜品");
        //动态构造key
        String key = "Dish_"+categoryId.toString();
        List<DishDto> list=null;
        //先从redis中获取缓存数据
        list = (List<DishDto>) redisTemplate.opsForValue().get(key);

        if(list != null){
            //如果存在，直接返回，无需查询数据库
            return R.success(list);
        }
        list=dishService.listDish(categoryId,status);
        //如果不存在，需要查询数据库，将查询到的菜品数据缓存到Redis
        redisTemplate.opsForValue().set(key,list,60, TimeUnit.MINUTES);
        return R.success(list);
    }
}
